﻿Imports System.Data.SqlClient

Public Class frmProducersRegistery

    Sub FillProducers()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From Producers Order By SNo", cnn)
            Dim Reader As SqlDataReader

            Me.GridProducersSalesmen.Rows.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Me.GridProducersSalesmen.Rows.Add(New String() {Reader.Item("SNo"), Reader.Item("FullName"), Reader.Item("MobileNo"), _
                                                                Reader.Item("Email"), Reader.Item("IDInfo"), Reader.Item("Address"), _
                                                                "Edit", "Delete"})
            End While
            cnn.Close()

            Me.lblResult.Text = "Count: " & Me.GridProducersSalesmen.Rows.Count

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub frmProducrsRegistery_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.WindowState = FormWindowState.Maximized
        FillProducers()

    End Sub

    Private Sub GridProducersSalesmen_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridProducersSalesmen.CellClick
        If e.ColumnIndex = 6 Then
            Dim a As New frmProducersEdit
            a.SNo = Me.GridProducersSalesmen.CurrentRow.Cells(0).Value
            a.ShowDialog()
            FillProducers()

        ElseIf e.ColumnIndex = 7 Then
            If MsgBox("Confirm delete?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                Try
                    Me.Cursor = Cursors.WaitCursor

                    Dim cmd As New SqlCommand("Delete From Producers Where SNo=" & _
                                              Me.GridProducersSalesmen.CurrentRow.Cells(0).Value, cnn)

                    cnn.Open()
                    cmd.ExecuteNonQuery()
                    cnn.Close()

                    FillProducers()

                    Me.Cursor = Cursors.Default
                Catch ex As Exception
                    Me.Cursor = Cursors.Default
                    If cnn.State = ConnectionState.Open Then
                        cnn.Close()
                    End If
                    MsgBox(ex.ToString)
                End Try
            End If
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnIns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIns.Click
        Dim a As New frmProducersNew
        a.ShowDialog()

        FillProducers()
    End Sub
End Class